<?php
/** 
* 短信发送 
* 
* 暂只支持注册发送验证码-后期可以扩展支持所有短信发送
* @author      Ray<jianglei@tiantu.in> 
* @version     $Id$ 
* @date  2016-4-14 17:17
* @since        1.0 
*/
$root = dirname(__FILE__);
$key = 'bdborder';
require_once $root.'/config.php';
require_once $root.'/mpdo/Db.class.php';
require_once $root.'/mpdo/Log.class.php';
$redis = redis_cluster::getInstance();
$order_no = $redis->lpop($key);
$Log = new Log();

if(empty($order_no)){
	sleep(10);exit;
}
$Log->write($order_no);
$dbchbc = new DB('slave');
$dbbdb = new DB('master');
$dbbdb->BeginTransaction();
try {
	// 订单表
	$order = $dbchbc->row("SELECT * FROM chbc_play_order WHERE play_order_no = ".$order_no);
	// 凭证表
	$proof = $dbchbc->row("SELECT * FROM chbc_play_order_proof WHERE play_order_no = ".$order_no." order by id DESC");
	// 同步数据
	// 活动方案
	if ($proof['three_image']) {
		$scheme_no = '图文反馈';
	} else {
		$scheme_no = '无需反馈';
	}
	// 手续费，结算金额，同步到爆单宝时应该显示的是爆单宝返给吃喝不愁的结算金额和手续费
	$play = $dbbdb->row("SELECT service_amt,service_admin_id,service_admin_by FROM chbc_play WHERE play_no = ".$order['play_no']);
	$bdborder = $dbbdb->row("SELECT order_status FROM chbc_play_order WHERE play_order_no = ".$order['play_order_no']);
	$service_charge_amt = $play['service_amt'];
	$actual_rebate_amt = $order['service_charge_amt']+$order['actual_rebate_amt'];
	$rebate_amt = $service_charge_amt+$actual_rebate_amt;
	if ($bdborder['order_status']!=6) {
		$dbbdb->query("INSERT INTO chbc_play_order SET play_order_no = '".$order["play_order_no"]."', user_id = '".$order["user_id"]."', play_no = '".$order["play_no"]."', shop_no = '".$order["shop_no"]."', shop_name = '".$order["shop_name"]."', platform_no = '".$order["platform_no"]."', rebate_amt = '".$rebate_amt."', service_charge_amt = '".$service_charge_amt."', actual_rebate_amt = '".$actual_rebate_amt."', place_time = '".$order["place_time"]."', expire_time = '".$order["expire_time"]."', pay_time = '".$order["pay_time"]."', is_pay = '".$order["is_pay"]."', proposal = '".$order["proposal"]."', crood_type = '".$order["crood_type"]."', lat = '".$order["lat"]."', order_status = '".$order["order_status"]."', lng = '".$order["lng"]."', create_by = '".$order["create_by"]."', create_dt = '".$order["create_dt"]."', update_by = '".$order["update_by"]."', update_dt = '".$order["update_dt"]."', user_pay = '".$order["user_pay"]."', location = '".$order["location"]."', cps_id = '".$order["cps_id"]."', get_coupon = '".$order["get_coupon"]."', wm_order_sn = '".$order["wm_order_sn"]."', is_fushen = '".$order["is_fushen"]."', is_yunzhan = '".$order["is_yunzhan"]."', scheme_no = '".$scheme_no."', service_admin_id = '".$play["service_admin_id"]."', service_admin_by = '".$play['service_admin_by']."'");
		$dbbdb->query("INSERT INTO chbc_play_order_proof SET play_order_no = '".$proof["play_order_no"]."', user_id = '".$proof["user_id"]."', shop_no = '".$proof["shop_no"]."', wm_order_sn = '".$proof["wm_order_sn"]."', platform_no = '".$proof["platform_no"]."', one_image = '".$proof["one_image"]."', two_image = '".$proof["two_image"]."', three_image = '".$proof["three_image"]."', back_money = '".$proof["back_money"]."', remarks = '".$proof["remarks"]."', handle_admin_id = '".$proof["handle_admin_id"]."', handle_admin_desc = '".$proof["handle_admin_desc"]."', handle_merchant_id = '".$proof["handle_merchant_id"]."', handle_merchant_desc = '".$proof["handle_merchant_desc"]."', handle_time = '".$proof["handle_time"]."', handle_status = '".$proof["handle_status"]."', machine_audit_state = '".$proof["machine_audit_state"]."', machine_audit_desc = '".$proof["machine_audit_desc"]."', create_dt = '".$proof["create_dt"]."', deduction_beans = '".$proof["deduction_beans"]."', update_dt = '".$proof["update_dt"]."', user_pay = '".$proof["user_pay"]."', financial_review = '".$proof["financial_review"]."', financial_review_num = '".$proof["financial_review_num"]."', aftersale_evidence = '".$proof["aftersale_evidence"]."', order_aftersale_id = '".$proof["order_aftersale_id"]."', score = '".$proof["score"]."', upload_time = '".$proof["upload_time"]."', supplement = '".$proof["supplement"]."', yc_remark = '".$proof["yc_remark"]."', yh_remark = '".$proof["yh_remark"]."', sj_remark = '".$proof["sj_remark"]."', actual_pay = '".$proof["actual_pay"]."', platform = '".$proof["platform"]."'");
		// 活动消耗库存
		$play = $dbbdb->row("SELECT chbcquota,stock,chbcconsume FROM chbc_play WHERE play_no = ".$order['play_no']);
		$chbcconsume = $play['chbcconsume']+1;
		$stock = $play['stock']-1;
		if ($stock<0) $stock=0;
		$dbbdb->query('UPDATE chbc_play SET stock = '.$stock.',chbcconsume='.$chbcconsume.' WHERE play_no = '.$order['play_no']);
	} else {
		$dbbdb->query('UPDATE chbc_play_order SET order_status = 3,actual_rebate_amt='.$order['actual_rebate_amt'].' WHERE play_order_no = '.$order['play_order_no']);
	}
	$dbbdb->Commit();
} catch (\Exception $e) {
	$dbbdb->RollBack();
}
usleep(200000);
?>
